import Vue from 'vue'
import VueRouter from 'vue-router'
import { rotertable } from '@/api/login'
Vue.use(VueRouter)
// 解决Vue-Router升级导致的Uncaught(in promise) navigation guard问题
const originalPush = VueRouter.prototype.push
VueRouter.prototype.push = function push(location, onResolve, onReject) {
  if (onResolve || onReject) return originalPush.call(this, location, onResolve, onReject)
  return originalPush.call(this, location).catch(err => err)
}
// 针对 replace 方法
VueRouter.prototype.replace = function push(location, onResolve, onReject) {
  if (onResolve || onReject) return originalReplace.call(this, location, onResolve, onReject)
  return originalReplace.call(this, location).catch(err => err)
}

const routes = [
  {
    path: '/',
    name: 'AdminLogin',
    component: () => import('@/views/AdminLogin'),
    hidden: true
  },
  {
    path: "*",
    name: '404',
    component: () => import('@/views/404.vue'),
  },
  {
    path: '/checkmajor',
    name: 'checkmajor',
    component: () => import('@/views/checkmajor'),
    hidden: true
  },
  // {
  //   path: '/HomePage',
  //   name: 'HomePage',
  //   // redirect:'/HomePage/QRcode',
  //   component: () => import('@/views/HomePage'),
  //   hidden: true,
  //   children:[
  //     {
  //       path: 'QRcode',
  //       name: 'QRcode',
  //       component: () => import('@/views/QRcode'),
  //       hidden: true,
  //     },
  //     {
  //       path: 'personinfo',
  //       name: 'personinfo',
  //       component: () => import('@/views/PersonInfo'),
  //       hidden: true,
  //     },
  //     {
  //       path: 'questManage',
  //       name: 'questManage',
  //       component: () => import('@/views/QuestManage'),
  //       hidden: true,
  //     },
  //     {
  //       path: 'statanal',
  //       name: 'statanal',
  //       component: () => import('@/views/StatAnal'),
  //       hidden: true,
  //     },
  // ]
  // },
  // {
  //   path: '/SystemManage',
  //   name: 'SystemManage',
  //   component: () => import('@/views/HomePage'),
  //   hidden: true,
  //   children:[
  //     {
  //       path: 'account',
  //       name: 'account',
  //       component: () => import('@/views/AccountManage'),
  //       hidden: true,
  //     },
  //     {
  //       path: 'PersonInfo',
  //       name: 'PersonInfo',
  //       component: () => import('@/views/PersonInfo'),
  //       hidden: true,
  //     },
  //     {
  //       path: 'role',
  //       name: 'role',
  //       component: () => import('@/views/RoleManage'),
  //       hidden: true,
  //     },
  //     {
  //       path: 'resource',
  //       name: 'resource',
  //       component: () => import('@/views/NatuResourc'),
  //       hidden: true,
  //     },
  //     {
  //       path: 'dicManage',
  //       name: 'dicManage',
  //       component: () => import('@/views/DicManage'),
  //       hidden: true,
  //     },
  // ]
  // },
]

const router = new VueRouter({
  // mode: 'history',
  mode: 'hash',
  // base: '/vue',
  base: process.env.BASE_URL,
  routes,
})


let mRoutes
router.beforeEach(async (to, form, next) => {
  const token = localStorage.getItem('token');
  if (token) {
    let res = await rotertable()
    mRoutes = res.data.data;
    // // 向仓库发送api的路由数据
    // console.log(mRoutes);
    if(mRoutes.length==0){
      next('/404')
    }else{
      if (to.name != 404) {
        // console.log(1234);
        //放行
        next()
      } else {
        // console.log(mRoutes, 454545);
        mRoutes.forEach((items, index) => {
          router.addRoute(
            {
              path: `/${items.resourceUrl}`,
              name: items.resourceUrl,
              redirect: '/HomePage/'+mRoutes[0].children[0].resourceUrl,
              component: () => import(`@/views/${items.component}`),
              children: items.children ? items.children.map(item => {
                return {
                  path: item.resourceUrl,
                  name: item.resourceUrl,
                  // redirect: item.resourceUrl,
                  component: () => import(`@/views/${item.component}`)
                }
              }) : []
            },
          )
          next({ path: `${to.path}` })
  
        });
        // console.log(router.getRoutes(), 13);
        // console.log(to.name, 14141414);
      }
    }

  } else {
    // 未登录时，注意 ：在这里也许你的项目不只有 logon 不需要登录 ，register 等其他不需要登录的页面也需要处理
    if (to.path !== '/') {
      next({ path: '/' })
    } else {
      next()
    }
  }
})

export default router